//
//  SCIntegralMallCell.m
//  shangcheng
//
//  Created by 张艳江 on 2019/5/29.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCIntegralMallCell.h"
#import "SCIntegralGoodsCell.h"

@interface SCIntegralMallCell()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UICollectionView *collectionView;

@end

@implementation SCIntegralMallCell

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        CGFloat w = (kScreenW-50)/3.0f;
        layout.itemSize = CGSizeMake(w, w*79/105+45);
        layout.sectionInset = UIEdgeInsetsMake(12, 12, 12, 12);
    
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerNib:[UINib nibWithNibName:@"SCIntegralGoodsCell" bundle:nil] forCellWithReuseIdentifier:@"SCIntegralGoodsCellID"];
    }
    return _collectionView;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self addSubview:self.collectionView];
    }
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.collectionView.frame = CGRectMake(0, 0, kScreenW, self.height);
}
- (void)setListArray:(NSArray *)listArray{
    _listArray = listArray;
    [self.collectionView reloadData];
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.listArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SCIntegralGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SCIntegralGoodsCellID" forIndexPath:indexPath];
    cell.model = self.listArray[indexPath.row];
    return cell;
}
- (CGFloat)geCellHeight{
    
    CGFloat H;
    if (self.listArray.count == 0) {
        return H = 0;
    }
    
    CGFloat w = (kScreenW-50)/3.0f;
    CGFloat h = w*79/105+45;
    if (self.listArray.count%3 == 0) {
        H = (h*self.listArray.count/3 + 12*(self.listArray.count/3+1));
    }else{
        H = (h*(self.listArray.count/3+1) + 12*(self.listArray.count/3+2));
    }
    
    return H;
}
#pragma mark - <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    if ([self.delegate respondsToSelector:@selector(clickIntegralMallGoodsModel:)]) {
        [self.delegate clickIntegralMallGoodsModel:self.listArray[indexPath.row]];
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
